package br.edu.ifrn.sgpapp.activity;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import br.edu.ifrn.sgp.dtos.NoticiaDTO;
import br.edu.ifrn.sgpapp.R;
import br.edu.ifrn.sgpapp.adapter.NoticiasListAdapter;
import br.edu.ifrn.sgpapp.rest.NoticiaREST;

public class ListarNoticiasActivity extends GenericActivity<List<NoticiaDTO>> {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lista_geral);
		processarSolicitacao();
	}
	
	@Override
	public List<NoticiaDTO> doInBackgroundTask(String... params) {
		return NoticiaREST.listarUltimasNoticias();
	}

	@Override
	public void onPostExecuteTask(List<NoticiaDTO> noticias) {
		final ListView listaNoticias = (ListView) findViewById(R.id.list);
		NoticiasListAdapter adapter = new NoticiasListAdapter(this, noticias);
		listaNoticias.setAdapter(adapter);
		listaNoticias.setEmptyView(findViewById(R.id.textEmptyView));
		
		listaNoticias.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(ListarNoticiasActivity.this, VerNoticiaActivity.class);
				intent.putExtra(getString(R.string.extra_noticia), (NoticiaDTO) listaNoticias.getAdapter().getItem(position));
				startActivity(intent);
			}
		});
	}
}
